package com.i.userservice.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.i.piccommon.domain.User;
import com.i.piccommon.util.JWTUtils;
import com.i.userservice.mapper.UserMapper;
import com.i.userservice.service.IUserService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lewis
 * @type class
 * @description
 * @since 2024/10/24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public Boolean registry(User user) {
        List<User> list = lambdaQuery().eq(User::getEmail, user.getEmail()).list();
        if(list.isEmpty()) {
            return save(user);
        }
        return false;
    }

    @Override
    public User login(User user) {
        User userOne = lambdaQuery().eq(User::getEmail, user.getEmail()).eq(User::getPassword, user.getPassword()).one();
        if(userOne != null) {
            String token = JWTUtils.createToken(userOne);
            userOne.setToken(token);
            return userOne;
        }else {
            return null;
        }
    }

    @Override
    public User info(Long userId) {
        return lambdaQuery().eq(User::getUid, userId).one();
    }

    @Override
    public User edit(User user) {
        boolean update = lambdaUpdate().update(user);
        if(!update) {
            return null;
        }
//        String token = JWTUtils.createToken(newUser);
//        newUser.setToken(token);
        return lambdaQuery().eq(User::getUid, user.getUid()).one();
    }

    @Override
    public Boolean logoff(Long userId) {
        return remove(lambdaQuery().eq(User::getUid,userId));
    }
}
